<?php
namespace app\talbum\service;
use app\talbum\model\Photo_album as pa;
use app\talbum\validata\TablumValidata as tv;
use app\base\service\BaseService;
use think\facade\Log;
class IndexService extends BaseService 
{
	protected $album ='';
	protected $tv='';

	 public function __construct()
	 {
	 	parent::__construct();
	 	$this->album = new pa;
	 	$this->tv = new tv;
	 }
	
/**
 * 时光相册_S
 */
	//增加相册
	protected function insert_album($data){
		if (!$this->tv->scene('add_album')->check($data)) {
            return $this->return_code(300,$this->tv->getError());
        }
        //判断权限 家人和自己可以添加
        $perm = $this->permissions($data['Openid']);
     	
        if($perm['code'] !=10000&&$perm['code']!=10001)   return $this->return_code(301,"对不起，权限不足");
        //通过Openid获得用户信息
        // if(isset($data['Openid'])&&!empty($data['Openid'])){
        // 	$userinfo = $this->get_userinfo($data['Openid']);
        // }else{
        // 	$userinfo = $this->user;
        // }
        $album['album']['createtime'] =time();
		$album['album']['Isuse'] =0;
		$album['album']['ordernum'] =0;
		$album['album']['Hits'] =0;
		$album['album']['Token'] =$data['Token'];
		$album['album']['Userid'] =$this->user["Userid"];
		$album['album']['Openid'] =$this->user["Openid"];
		$album['album']['albumname'] =$data["albumname"];
		$album['album']['describe'] =$data["describe"];
		$album['album']['permissions'] =$data["permissions"];
		$album['album']['albumregion'] =$data["albumregion"];
		$album['image'] = array();
		if(isset($data['albumcover'])&&!empty($data['albumcover'])&&!is_null($data['albumcover'])){
			$image=$this->upload_one_img($data['albumcover']);
			if(!$image['code']==200)  return $this->return_code($image['code'],$image['msg']);
			$album['image'] = array(
				'picsname'=>'',
	        	'Picspath'=>$image['imagepath'],
	        	'Openid'=>$this->user['Openid'],
				'createtime'=>time(),
				'Userid'=>$this->user['Userid'],
				'Iscover'=>1,
				'ordernum'=>0 
			);
		}
		
        $id = $this->album->insert_album($album);
        if($id){
        	 return $this->return_code(200,'时光相册创建成功');
        }
        return  $this->return_code();
	}
	//查找时光相册列表
	protected function get_album($data){
		if (!$this->tv->scene('page')->check($data)) {
            return $this->return_code(300,$this->tv->getError());
        }
         //判断权限 家人和自己可以添加
        $perm = $this->permissions($data['Openid']);
        if($perm['code'] ==10004&&$perm['code']==10002)   return $this->return_code(301,"对不起，权限不足");
        //通过Openid获得用户信息
        if(isset($data['Openid'])&&!empty($data['Openid'])){
        	$userinfo = $this->get_userinfo($data['Openid']);
        }else{
        	$userinfo = $this->user;
        }
          switch ($perm['code']) {
        	case '10000':
        		
        		break;
   			case '10001'://家人
        		 $where['p.permissions']   = array('neq' => 3);
        		break;
		   	case '10002'://好友
				$where['p.permissions']   = array('neq' => 3);
			break;
        	
        	default:
        		$where['p.permissions']   = 1;
        		break;
        }
        $where['p.Token']=(isset($data['Token'])&&!empty($data['Token'])) ? $data['Token']:$userinfo['Token'];
        if(!empty($data['albumid'])&&!is_null($data['albumid']))
        $where['p.albumid']=$data['albumid'];
        $where['p.status']=1;
      //  $where['pa.Iscover']=1;
        $res = $this->album->album_list($data['page'],$data['limit'],$where);
        return $this->return_code(200,'获取数据成功',$res);
	}

	//编辑保存相册
	public function edit_album($data){
		if (!$this->tv->scene('add_album')->check($data)) {
            return $this->return_code(300,$this->tv->getError());
        }
           //判断权限 家人和自己可以添加
        $perm = $this->permissions($data['Openid']);

        if($perm['code'] !=10000&&$perm['code']!=10001)   return $this->return_code(301,"对不起，权限不足",$perm);
        //通过Openid获得用户信息
        // if(isset($data['Openid'])&&!empty($data['Openid'])){
        // 	$userinfo = $this->get_userinfo($data['Openid']);	
        // }else{
        // 	$userinfo = $this->user;
        // }
        //判断是否是自己建的相册
        $where_c['Openid'] = $this->user['Openid'];
        $where_c['Token'] = $data['Token'];
        $where_c['albumid'] = $data['albumid'];
        $counta = $this->album->countWhere('Photo_album',$where_c);
        if($counta<=0)  return $this->return_code(302,"对不起，权限不足",$where_c);
        
        $album['album']['albumid'] =$data['albumid'];
        $album['album']['createtime'] =time();
		$album['album']['Isuse'] =0;
		$album['album']['ordernum'] =0;
		// $album['album']['Hits'] =$data["Hits"];
		$album['album']['albumname'] =$data["albumname"];
		$album['album']['describe'] =$data["describe"];
		$album['album']['permissions'] =$data["permissions"];
		// $album['album']['albumregion'] =$data["albumregion"];
		$album['image'] = array();
	
		if(isset($data['albumcover'])&&!empty($data['albumcover'])&&!is_null($data['albumcover'])){
			$image=$this->upload_one_img($data['albumcover']);
			
			if(!$image['code']==200)  return $this->return_code($image['code'],$image['msg']);
			$album['image'] = array(
				'albumid'=>$data['albumid'],
				'picsname'=>'',
	        	'Picspath'=>$image['imagepath'],
	 
				'createtime'=>time(),
				
				'Iscover'=>1,
				'ordernum'=>0 
			);
		}
        $id = $this->album->edit_album($album);

        if($id){
        	 return $this->return_code(200,'相册编辑成功');
        }
        return  $this->return_code();
	}

	//上传相册照片
	protected function upload_photo($data){
		
		$data['image']=[];

		//相册id 描述 图片
		if (!$this->tv->scene('upload_desc')->check($data)) {
            return $this->return_code(300,$this->tv->getError());
        }
               //判断权限 
        $perm = $this->permissions($data['Openid']);
        if($perm['code'] !=10000&&$perm['code']!=10001)   return $this->return_code(301,"对不起，权限不足");
        //通过Openid获得用户信息
        // if(isset($data['Openid'])&&!empty($data['Openid'])){
        // 	$userinfo = $this->get_userinfo($data['Openid']);
        // }else{
        // 	$userinfo = $this->user;
        // }
        //dump($userinfo);
        //判断是否是自己建的相册
        $where_c['Openid'] = $this->user['Openid'];
        $where_c['Token'] = $data['Token'];
        $where_c['albumid'] = $data['albumid'];
        $counta = $this->album->countWhere('Photo_album',$where_c);

        
        if($counta<=0)  return $this->return_code(301,"对不起，");
        //上传图片
        $imageres=$this->upload_img($data['imagefile']);
        //log::write($imageres);
        if($imageres['code']==200){
        	//操作数据库
        	foreach ($imageres['imagepath'] as $key => $value) {
        		$data['image'][]=[
        			'picsname'=>(isset($data['picsname'][$key])&&!empty($data['picsname'][$key]))?$data['picsname'][$key]:'',
        			'Picspath'=>$value,
        			'Openid'=>$this->user['Openid'],
					'createtime'=>time(),
					'Userid'=>$this->user['Userid'],
					'Iscover'=>(isset($data['Iscover'][$key])&&$data['Iscover'][$key]==1)?1:0,
					'albumid'=>$data['albumid'],
					'ordernum'=>0
					
        		];
        	}

        	if($this->album->upload_photo($data)) return $this->return_code(200,'成功');

        }else{
        	return $this->return_code(300,$imageres['msg']);
        }

        return $this->return_code();
	}

	//查看相册内图片列表接口STATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '10 FROM xj_picsid,picsname,_openid,createtime, pdescribe,_userid,_picspath LIMIT' at line 1
	public function get_album_photos($data)
	{
		if (!$this->tv->scene('get_album_photos')->check($data)) {
            return $this->return_code(300,$this->tv->getError());
        }
            //判断权限 
       

        $where['albumid']  =$data['albumid'];
          $where['status']=1;
        $res = $this->album->loadAllData("photo_album_pics","picsid,picsname,Openid,createtime, pdescribe,Userid,Picspath",$where,$data['page'],$data['limit'],"picsid desc");
          return $this->return_code(200,'获取数据成功',$res);
	}


	//删除图片
	public function delete_album_photos($data){
		if (!$this->tv->scene('delete_album_photo')->check($data)) {
            return $this->return_code(300,$this->tv->getError());
        }
             //判断权限 
        $perm = $this->permissions($data['Openid']);
        if($perm['code'] !=10000&&$perm['code']!=10001)   return $this->return_code(301,"对不起，权限不足");
     	$data['Openid'] = $this->user['Openid'];
        // $photo = $this->album->find_photo($data);
        // if(empty($photo)) return $this->return_code(400,'图片不存在');
        //删除数据库
        $res = $this->album->del_album_photo($data);
        //删除图片文件
        // $res_image_code =$this->del_file($photo['Picspath']);
   
        if($res) return $this->return_code(200,'图片删除成功');
        return $this->return_code();

	}
	//批量删除图片
	public function del_many_photos($data){
		if (!$this->tv->scene('del_many_photos')->check($data)) {
            return $this->return_code(300,$this->tv->getError());
        }
        $data['Openid'] = $this->user['Openid'];
        $photo = $this->album->find_photos($data);
    
        //循环删除本地图片
        $where = '';
        foreach ($photo as $k => $v) {
        	 // $res_image_code =$this->del_file($v['Picspath']);
        	 // if($res_image_code) 
              $where .= $v['picsid'].",";
        }
          
         if($where=='') return $this->return_code(301,'不存在此图片');
        //删除数据库相关信息
         $res = $this->album->del_many_photos($where);
         if($res) return $this->return_code(200,'图片删除成功');
         return $this->return_code();

	}

	//删除相册
	public function del_albums($data){
		if (!$this->tv->scene('del_albums')->check($data)) {
            return $this->return_code(300,$this->tv->getError());
        }
        $where_p['albumid'] = $data['albumid'];
        $where_p['Openid'] = $this->user['Openid'];
       $photo = $this->album->findAllByWhere('photo_album_pics',$where_p,'picsid,Picspath','picsid desc');
        //循环删除本地图片
        // foreach ($photo as $k => $v) {
        // 	 $res_image_code =$this->del_file($v['Picspath']);
        // }
          
        //删除数据库相关信息
         $res = $this->album->del_albums($where_p);
         if($res) return $this->return_code(200,'相册删除成功');
         return $this->return_code();
	}
	//获得单张图片的评论数，和点赞数 get_count
	protected function get_count($data){
		if (!$this->tv->scene('delete_album_photo')->check($data)) {
            return $this->return_code(300,$this->tv->getError());
        }
        $res = $this->album->get_count($data);
        return $res;

	}


	//查看评论
	  public function comment_list($data){
	  	if (!$this->tv->scene('comment_list')->check($data)) {
            return $this->return_code(300,$this->tv->getError());
        }

        $res = $this->album->comment_list($data,$data['page'],$data['limit']);
        return $res;
	  }

	  //添加评论
	  	protected function comments($data){
		if (!$this->tv->scene('comment')->check($data)) {
            return $this->return_code(300,$this->tv->getError());
        }
        $comment=[
        	'content'=>$data['content'],
        	'Openid' =>$this->user['Openid'],
        	'createtime'=>time(),
        	'Userid' => $this->user['Userid'],
        	'ordernum' =>0,
        	'Hits'=>0,
        	'number'=>0,
        	'picsid'=>$data['picsid'],
        	'Token'=>$this->user['Token'],
        	'Replayid'=>$data['Replayid']
        ];
         $id = $this->album->comment($comment);
         if($id){
        	 return $this->return_code(200,'评论成功');
        }
        return  $this->return_code();

	}

	  //点赞和取消
        protected function add_points($data){
        if (!$this->tv->scene('add_points')->check($data)) {
            return $this->return_code(300,$this->tv->getError());
        }
        $where['Openid'] = $this->user['Openid'];
        $where['picsid'] = $data['picsid'];

        $point = $this->album->findByAttributes('photo_point',$where,'status');
     if(empty($point)){
        $point=[
            'Openid' =>$this->user['Openid'],
            'createtime'=>time(),
            'Userid' => $this->user['Userid'],
            'albumid' =>$data['albumid'],
            'status' =>1,
            'trackid'=>$data['trackid'],
        ];
         $id = $this->album->add_points($point);
         if($id){
             return $this->return_code(200,'点赞成功');
        }
     }else{
        if($point['status'] == 1){
            $up['status']=2; 
        }else{
            $up['status']=1; 
        }
          $up_p = $this->album->updateByWhere('photo_point',$where,$up);
          if($up_p)   return $this->return_code(200,'成功');
     }
        
        
        return  $this->return_code();

    }
}
?>